package com.jcsz.gulimall.search.thread;

import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

/**
 * @className: ThreadTestCase2
 * @author: 一把Po吉他
 * @date: 2022/11/6 9:05
 * @version: 1.0
 **/
public class ThreadTestCase2 {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main start....");
//        CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果:" + i);
//        }, executorService);
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                return 10 / 0;
//            }
//        }, executorService).whenCompleteAsync(new BiConsumer<Integer, Throwable>() {
//            @Override
//            public void accept(Integer integer, Throwable throwable) {
//                System.out.println(integer + "---" + throwable);
//            }
//        }, executorService).exceptionally((exc) -> {
//            return 100000;
//        });
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                return 10 / 2;
//            }
//        }, executorService).handleAsync((res, exc) -> {
//            if (res != null) {
//                return res;
//            }
//            if (exc != null) {
//                return 9999;
//            }
//            return 8888;
//        },executorService);
//        CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                return 10 / 2;
//            }
//        }, executorService).thenRunAsync(() -> {
//            System.out.println("任务2.........");
//        }, executorService);
        //Integer integer = integerCompletableFuture.get();

//        CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                return 10 / 2;
//            }
//        }, executorService).thenAcceptAsync((res)->{
//            System.out.println(res+"任务2222.。。。");
//        }, executorService);
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                return 10 / 2;
//            }
//        }, executorService).thenApplyAsync((res) -> {
//            System.out.println("任务2..........." );
//            return res * 77777;
//        }, executorService);
//        Integer integer = integerCompletableFuture.get();
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1.......start");
            return 1;
        }, executorService);
        CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2.......start");
            return 2;
        }, executorService);
//        future01.runAfterBothAsync(future02, () -> {
//            System.out.println("任务3.......start");
//        }, executorService);
//        future01.thenAcceptBothAsync(future02, (res1, res2) -> {
//            System.out.println("任务3.......start之前的结果" + res1 + res2);
//        }, executorService);
//        CompletableFuture<Integer> future03 = future01.thenCombineAsync(future02, (res1, res2) -> {
//            System.out.println("任务3.......start之前的结果" + res1 + res2);
//            return res1 + res2;
//        }, executorService);
//        future01.runAfterEitherAsync(future02, () -> {
//            System.out.println("任务3.......start");
//        }, executorService);
//        future01.acceptEitherAsync(future02, (res) -> {
//            System.out.println("任务3.......start"+res);
//        }, executorService);
        CompletableFuture<Integer> future = future01.applyToEitherAsync(future02, (res) -> {
            System.out.println("任务3.......start" + res);
            return 3;
        }, executorService);
        System.out.println("main end...."+future.get());
    }
}
